package com.example.ble_demo

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.graphics.alpha
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.ble_demo.adapter.BlueToothAdapter
import com.example.ble_demo.adapter.DeviceInfoBeanDiff
import com.example.ble_demo.databinding.FragmentDeviceListBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class DeviceListFragment : Fragment() {
    private val mTAG = "DeviceListFragment"


    private lateinit var viewModel: DeviceListViewModel
    private lateinit var binding: FragmentDeviceListBinding


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentDeviceListBinding.inflate(inflater, container, false)
        initView()
        return binding.root
    }

    private fun initView() {
        val adapter = BlueToothAdapter(DeviceInfoBeanDiff())

        adapter.onItemClickListener = {
            GlobalScope.launch {
                Log.d(mTAG, "onItemClick: $it")
                (activity as MainActivity).bondDevice(it)
            }
        }
//        adapter.onItemLongClickListener = { deviceInfoBean ->
//            Log.d(mTAG, "onItemLongClick: $deviceInfoBean")
//            GlobalScope.launch {
//                var socket = (activity as MainActivity).connectDevice(deviceInfoBean)
//                if (socket != null) {
//                 while (true) {
//                     var inputStream = socket.inputStream
//                     var outputStream = socket.outputStream
//                     var buffer = ByteArray(1024)
//                     var bytes: Int
//                     while (inputStream.read(buffer).also { bytes = it } != -1) {
//                         val readMessage = String(buffer, 0, bytes)
//                         outputStream.write("I got it".toByteArray())
//                         Log.d(mTAG, "收到服务器消息: $readMessage")
//                     }
//                 }
//                }
//                withContext(Dispatchers.Main) {
//                    if (socket != null) {
//                        Toast.makeText(context, "连接成功", Toast.LENGTH_SHORT).show()
//                    } else {
//                        Toast.makeText(context, "连接失败", Toast.LENGTH_SHORT).show()
//                    }
//                }
//            }
//
//        }
        adapter.onItemLongClickListener = { deviceInfoBean ->
            Log.d(mTAG, "onItemLongClick: $deviceInfoBean")
            GlobalScope.launch {
                var gatt = (activity as MainActivity).connectGattService(deviceInfoBean)

            }

        }
        binding.devicesListView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        binding.devicesListView.adapter = adapter
        Log.d(mTAG, "initView:${viewModel.deviceList.value?.size} ")
        viewModel.deviceList.observe(viewLifecycleOwner) {
            adapter.submitList(it)
        }

    }


    override fun onAttach(context: Context) {
        super.onAttach(context)
        viewModel = (activity as MainActivity).viewModel
    }

}